package com.wjy;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeAutoGenerator {

    private static String moduleName;
    private static String include;

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入模块名");
            moduleName = scanner.next();
            if (StringUtils.isEmpty(moduleName)) {
                throw new MybatisPlusException("请输入正确的模块名");
            }
            System.out.println("请输入表名");
            include = scanner.next();
            if (StringUtils.isEmpty(include)) {
                throw new MybatisPlusException("请输入正确的表名");
            }

            generate();
        }
    }

    private static void generate() {
        AutoGenerator generator = new AutoGenerator();

        generator.setGlobalConfig(getGlobalConfig());
        generator.setDataSource(getDataSourceConfig());
        generator.setStrategy(getStrategyConfig());
        generator.setPackageInfo(getPackageConfig());
        generator.setTemplate(getTemplateConfig());
        generator.setCfg(getInjectionConfig());

        generator.setTemplateEngine(new FreemarkerTemplateEngine());

        generator.execute();
    }

    /*
     * 全局配置
     */
    private static GlobalConfig getGlobalConfig() {
        GlobalConfig config = new GlobalConfig();

        config.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        config.setFileOverride(true);
        config.setOpen(false);

        config.setAuthor("ybxxszl");
        config.setSwagger2(true);
        config.setIdType(IdType.UUID);
        config.setDateType(DateType.ONLY_DATE);
        config.setBaseResultMap(true);
        config.setBaseColumnList(true);
        config.setEnableCache(true);

        return config;
    }

    /*
     * 数据源配置
     */
    private static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig config = new DataSourceConfig();

        config.setDbType(DbType.MYSQL);
        config.setDriverName("com.mysql.jdbc.Driver");
        config
                .setUrl("jdbc:mysql://127.0.0.1:3306/rbac?useSSL=true&rewriteBatchedStatements=true&serverTimezone=GMT%2B8");
        config.setUsername("root");
        config.setPassword("1234");

        return config;
    }

    /*
     * 数据库表配置
     */
    private static StrategyConfig getStrategyConfig() {
        StrategyConfig config = new StrategyConfig();

        // 去除表前缀
        config.setTablePrefix("bd", "re");
        // 去除字段前缀
        config.setFieldPrefix();

        config.setSuperEntityClass("com.wjy.common.base.entity.BaseEntity");
        config.setSuperControllerClass("com.wjy.common.base.controller.BaseController");

        config.setNaming(NamingStrategy.underline_to_camel);
        config.setColumnNaming(NamingStrategy.underline_to_camel);

        config.setEntityLombokModel(true);
        config.setEntityColumnConstant(true);
        config.setEntityTableFieldAnnotationEnable(true);

        config.setRestControllerStyle(true);
        config.setControllerMappingHyphenStyle(false);

        config.setSkipView(false);

        config.setLogicDeleteFieldName("logic_delete");
        config.setVersionFieldName("version");

        // 自动填充字段
        List<TableFill> tableFillList = new ArrayList<TableFill>();

        tableFillList.add(new TableFill("logic_delete", FieldFill.INSERT));
        tableFillList.add(new TableFill("version", FieldFill.INSERT));
        tableFillList.add(new TableFill("insert_id", FieldFill.INSERT));
        tableFillList.add(new TableFill("insert_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_id", FieldFill.UPDATE));
        tableFillList.add(new TableFill("update_time", FieldFill.UPDATE));

        config.setTableFillList(tableFillList);

        // 不配置，将所有表生成
        config.setInclude(include);

        return config;
    }

    /*
     * 包名配置
     */
    private static PackageConfig getPackageConfig() {
        PackageConfig config = new PackageConfig();

        config.setParent("com.wjy");
        config.setModuleName(moduleName);

        return config;
    }

    /*
     * 模板配置
     */
    private static TemplateConfig getTemplateConfig() {
        TemplateConfig config = new TemplateConfig();

        // 去除官方xml路径，使用自定义路径
        config.setXml(null);

        return config;
    }

    /*
     * 自定义配置
     */
    private static InjectionConfig getInjectionConfig() {
        InjectionConfig config = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };

        String freemarkerTemplatePath = "/templates/mapper.xml.ftl";

        // 自定义输出配置
        List<FileOutConfig> list = new ArrayList<FileOutConfig>();

        list.add(new FileOutConfig(freemarkerTemplatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return System.getProperty("user.dir") + "/src/main/resources/mapper/" + moduleName + "/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        config.setFileOutConfigList(list);

        return config;
    }

}
